//
//  LFHomeViewController.m
//  LoveFruit
//
//  Created by 张令林 on 16/9/22.
//  Copyright © 2016年 张令林. All rights reserved.
//
#import "LFHotSellerModel.h"
#import "LFHomeViewController.h"
#import "PGIndexBannerSubiew.h"
#import "NewPagedFlowView.h"
#import "LFHomeLayout.h"
#import "LFFocusModel.h"
#import "LFIconModel.h"
#import "LFHotSellerModel.h"
#import "LFHomeHotSellCell.h"
#import "LFHomeWebViewController.h"
#import "LFHomeSearchViewController.h"
#import "LFFoodDetailController.h"
#import "LFMarketViewController.h"
//扫一扫头文件
#import "LFScanToolController.h"
//地址选择头文件
#import "HXProvincialCitiesCountiesPickerview.h"
#import "HXAddressManager.h"
//添加头部的滚动视图
#import "AutoScrollController.h"

#import "LFLoadSiteController.h"

#define Width [UIScreen mainScreen].bounds.size.width
static NSString *cellId = @"cellId";
static NSString *headerid = @"headerid";
static NSString *secondHeaderid = @"secondHeaderid";
static NSString *hotSellerCellid = @"hotsellerCellid";



@interface LFHomeViewController ()<NewPagedFlowViewDelegate, NewPagedFlowViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,LFScanToolControllerDelegate>
/*轮播器图片数组*/
@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic,weak) UICollectionView *collectionView;
//首页需要的数据
//第一个cell的数据
@property (nonatomic,strong) NSArray *focusArray;
//头部视图中按钮的数组
@property (nonatomic,strong) NSArray *iconsArray;
//第二个cell的数据
@property (nonatomic,strong) NSArray *hotSellerArray;
//地址选择的属性
@property (nonatomic,weak) UIButton *siteButton;
@property (nonatomic,strong) NSString *selectedSite;
@property (nonatomic,strong) HXProvincialCitiesCountiesPickerview *regionPickerView;

//头部三个按钮
@property (nonatomic,weak) UIButton *scanButton;
@property (nonatomic,weak) UIButton *searchButton;


@end

@implementation LFHomeViewController

//视图将要出现的时候添加头部的三个按钮
-(void)viewWillAppear:(BOOL)animated
{
    [self setTopButton];
    [self scrollViewDidScroll:self.collectionView];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithWhite:0 alpha:0];

    //加载数据
    [self loadData];
    //调用初始化方法
    [self setUpUI];
    //让导航控制器透明
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    
    //首次地址选择的时候执行的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectCity) name:@"isFirstTimeSelectCity" object:nil];
}
- (void)selectCity
{
    NSString *selCity = [[NSUserDefaults standardUserDefaults] objectForKey:@"firstLoadCity"];
    if (selCity != nil)
    {
        NSString *str = [NSString stringWithFormat:@"配送至:%@",selCity];
        [self.siteButton setTitle:str forState:UIControlStateNormal];
        [self.siteButton setTitle:str forState:UIControlStateHighlighted];
    }
    
}

//视图将要消失的时候移除头部的三个按钮
-(void)viewWillDisappear:(BOOL)animated
{
    [self.scanButton removeFromSuperview];
    [self.siteButton removeFromSuperview];
    [self.searchButton removeFromSuperview];
}
#pragma mark - 添加导航控制器上面的按钮
- (void)setNavButton
{
    //扫一扫
    UIButton *scan = [UIButton buttonTBImgName:@"icon_white_scancode" imageWH:15 title:@"" forSize:12 color:[UIColor darkGrayColor] spacing:1 target:self action:@selector(navBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    scan.size = CGSizeMake(30, 30);
    scan.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    [scan setCornerRadius:15 alpha:1];
    scan.tag = 1;
    UIBarButtonItem *scanBtnItem = [[UIBarButtonItem alloc] initWithCustomView:scan];
    self.navigationItem.leftBarButtonItem = scanBtnItem;
    //中间配送到哪的view
    NSString *siteStr = self.selectedSite ==nil?@"配送至:你在哪里啊":self.selectedSite;
    UIButton *button = [UIButton buttonLRWithImgName:@"icon_location" title:siteStr color:[UIColor whiteColor] forSize:12 target:self action:@selector(navBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 140, 30);
    button.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    [button setCornerRadius:8 alpha:1];
    button.tag = 2;
    self.siteButton = button;
    self.navigationItem.titleView = button;
    
    //搜索
    UIButton *search = [UIButton buttonTBImgName:@"icon_search_white" imageWH:15 title:@"" forSize:12 color:[UIColor darkGrayColor] spacing:1 target:self action:@selector(navBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    search.size = CGSizeMake(30, 30);
    search.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    [search setCornerRadius:15 alpha:1];
    search.tag = 3;
    UIBarButtonItem *searchBtnItem = [[UIBarButtonItem alloc] initWithCustomView:search];
    self.navigationItem.rightBarButtonItem = searchBtnItem;
}
// 加号
- (void)addBtnClick:(NSNotification *)notifcation {
    UIImageView *imageView = notifcation.userInfo[@"imageView"];
    CGFloat sizeH = self.tabBarController.tabBar.frame.origin.y + 23;
    CGFloat sizeW = self.tabBarController.tabBar.frame.size.width / 5 * 3.5 ;
    CGPoint end = CGPointMake(sizeW, sizeH);
    [self addProductsAnimation:imageView dropToPoint:end isNeedNotification:YES];

}
#pragma mark 初始化方法
- (void)setUpUI
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addBtnClick:)  name:@"model" object:nil];
    self.view.backgroundColor = [UIColor grayColor];
    self.title = @"";
    self.tabBarItem.title = @"首页";
    //创建collectionView
    LFHomeLayout *layout = [[LFHomeLayout alloc] init];
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    collectionView.backgroundColor = HMColor(239, 239, 239);
    self.collectionView = collectionView;
    [self.view addSubview:collectionView];
    //设置代理和数据源
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    //注册单元格
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellId];
    [self.collectionView registerNib:[UINib nibWithNibName:@"LFHomeHotSellCell" bundle:nil] forCellWithReuseIdentifier:hotSellerCellid];
    //添加collectionView的头部视图
    // 注册组头视图
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerid];
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:secondHeaderid];
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(-64);
        make.bottom.left.right.offset(0);
    }];
}
#pragma mark 添加上部的三个按钮
- (void)setTopButton
{
    //扫一扫
    UIButton *scan = [UIButton buttonTBImgName:@"icon_white_scancode" imageWH:15 title:@"" forSize:12 color:[UIColor darkGrayColor] spacing:1 target:self action:@selector(navBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    scan.size = CGSizeMake(30, 30);
    scan.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    [scan setCornerRadius:15 alpha:1];
    scan.tag = 1;
    //中间配送到哪的view
    NSString *siteStr = self.selectedSite ==nil?@"配送至:你在哪里啊":self.selectedSite;
    UIButton *button = [UIButton buttonLRWithImgName:@"icon_location" title:siteStr color:[UIColor whiteColor] forSize:12 target:self action:@selector(navBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 160, 30);
    button.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    [button setCornerRadius:8 alpha:1];
    button.tag = 2;
    self.siteButton = button;
    //搜索
    UIButton *search = [UIButton buttonTBImgName:@"icon_search_white" imageWH:15 title:@"" forSize:12 color:[UIColor darkGrayColor] spacing:1 target:self action:@selector(navBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    search.size = CGSizeMake(30, 30);
    search.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    [search setCornerRadius:15 alpha:1];
    search.tag = 3;
    
    //设置全局变量
    self.scanButton = scan;
    self.searchButton = search;
    
    //添加到当前视图
    scan.frame = CGRectMake(15, 15, 30, 30);
    button.y = 15;
    button.centerX = self.view.centerX;
    search.frame = CGRectMake(self.view.width-55, 15, 30, 30);
    
    [self.navigationController.navigationBar addSubview:scan];
    [self.navigationController.navigationBar addSubview:button];
    [self.navigationController.navigationBar addSubview:search];

}
#pragma mark - 加载首页数据的方法
- (void)loadData
{
    //加载上面部分的数据
    NSString *urlString = @"focus.json.php";
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@"1" forKey:@"call"];
    [NetworkingManager postUrlString:urlString withParam:params withSuccessBlock:^(id data) {
        NSDictionary *dict = data[@"data"];
        self.focusArray = [NSArray yy_modelArrayWithClass:[LFFocusModel class] json:dict[@"focus"]];
        self.iconsArray = [NSArray yy_modelArrayWithClass:[LFIconModel class] json:dict[@"icons"]];
        //回到主线程刷新数据
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
        
    } withFailedBlock:^(NSError *error) {
        NSString *fail = [NSString stringWithFormat:@"加载数据失败:%@",error];
        [SVProgressHUD showErrorWithStatus:fail];
    } withErrorBlock:^(NSString *message) {
        NSString *error = [NSString stringWithFormat:@"加载数据出错:%@",message];
        [SVProgressHUD showErrorWithStatus:error];
    }];
    //加载第二部分的数据
    NSString *urlStringSec = @"firstSell.json.php";
    NSMutableDictionary *paramsSec = [NSMutableDictionary dictionary];
    [paramsSec setValue:@"2" forKey:@"call"];
    [NetworkingManager postUrlString:urlStringSec withParam:paramsSec withSuccessBlock:^(id data) {
        NSArray *array = data[@"data"];
        self.hotSellerArray = [NSArray yy_modelArrayWithClass:[LFHotSellerModel class] json:array];
//        NSLog(@"%zd-----------------",self.hotSellerArray.count)
        //回到主线程刷新数据
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
        
    } withFailedBlock:^(NSError *error) {
        NSString *fail = [NSString stringWithFormat:@"加载数据失败:%@",error];
        [SVProgressHUD showErrorWithStatus:fail];
    } withErrorBlock:^(NSString *message) {
        NSString *error = [NSString stringWithFormat:@"加载数据出错:%@",message];
        [SVProgressHUD showErrorWithStatus:error];
    }];
    
    
}


#pragma mark - collectionView的数据源/代理方法
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return self.focusArray.count;
    }
    return self.hotSellerArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0)
    {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
        LFFocusModel *model = self.focusArray[indexPath.row];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.bounds];
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.img]];
        cell.backgroundView = imageView;
        return cell;
    }
    LFHomeHotSellCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:hotSellerCellid forIndexPath:indexPath];
    cell.model = self.hotSellerArray[indexPath.row];
    return cell;
    
}
//设置不同cell的size
- (CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath*)indexPath
{
    if (indexPath.section == 0) {  //第0组
        return CGSizeMake(collectionView.bounds.size.width - 10, 140);
    }
    
    return CGSizeMake((collectionView.bounds.size.width * 0.5 - 15) , 280);
}
//cell的点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"点击了%zd组%zd行的cell",indexPath.section,indexPath.row);
    if (indexPath.section == 0)
    {
        LFHomeWebViewController *webVC = [[LFHomeWebViewController alloc] init];
        webVC.focusModel = self.focusArray[indexPath.row];;
        [UIView animateWithDuration:1 animations:^{
            self.view.alpha = 0;
            self.navigationController.navigationBar.alpha = 0;
        } completion:^(BOOL finished) {
            [self.navigationController pushViewController:webVC animated:NO];
            self.view.alpha = 1;
            self.navigationController.navigationBar.alpha = 1;
        }];
        
    }else
    {
        LFFoodDetailController *detailVc = [[LFFoodDetailController alloc] init];
        detailVc.sellerModel = self.hotSellerArray[indexPath.row];
        
        [UIView animateWithDuration:1 animations:^{
            self.view.alpha = 0;
            self.navigationController.navigationBar.alpha = 0;
        } completion:^(BOOL finished) {
            [self.navigationController pushViewController:detailVc animated:NO];
            self.view.alpha = 1;
            self.navigationController.navigationBar.alpha = 1;
        }];
    }
    
}
//监听滚动的方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offSetY = scrollView.contentOffset.y;
//    NSLog(@"%f------",offSetY);
    if (offSetY > 150)
    {
        [UIView animateWithDuration:0.25 animations:^{
            self.scanButton.x = self.view.width-90;
            self.siteButton.x = self.view.width-60;
            self.siteButton.width = 30;
            [self.siteButton setCornerRadius:15 alpha:1];
            self.searchButton.x = self.view.width-30;
        }];
    }else
    {
        [UIView animateWithDuration:0.25 animations:^{
            self.scanButton.x = 15;
            self.siteButton.centerX = self.view.centerX;
            self.siteButton.width = 160;
            [self.siteButton setCornerRadius:8 alpha:1];
            self.searchButton.x = self.view.width-55;
        }];
    }
    

}
#pragma mark - collectionView的头部视图
//  返回头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    //如果是头视图
    if ([kind isEqualToString:UICollectionElementKindSectionHeader])
    {
        if (indexPath.section == 0 && indexPath.row == 0)
        {
            UICollectionReusableView *header=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerid forIndexPath:indexPath];
            //添加头部的滚动的视图
            //下面的代码只需要执行一次即可,后面都是复用,因为里面的内容不需要改变,所以复用直接用原来的,理论上应该重新赋值,不过这里偷懒了
            //添加头视图的内容,添加轮播器的方法
            [self setCycleView:header];
            //添加四个按钮
            [self setHeaderViewButton:header];
            //添加头条的滚动
            [self setNewsScrollView:header];
            return header;
        }else
        {
            UICollectionReusableView *secondHeader=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:secondHeaderid forIndexPath:indexPath];
            //下面的代码只需要执行一次即可,后面都是复用,因为里面的内容不需要改变,所以复用直接用原来的,理论上应该重新赋值,不过这里偷懒了
            
            
            //給视图添加内容
            [self setSecondHeader:secondHeader];
            secondHeader.backgroundColor = [UIColor whiteColor];

            
            return secondHeader;
        }
        
    }
    return nil;
}
//返回不同组的组头高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(self.view.frame.size.width, 278);//250
    }else {
        return CGSizeMake(self.view.bounds.size.width, 30);
    }
}
#pragma mark 添加头部视图中的四个按钮
- (void)setHeaderViewButton:(UICollectionReusableView*)headView
{
    UIStackView *stackView = [[UIStackView alloc] init];
    stackView.frame = CGRectMake(0, 185, SCREEN_W, 60);
    stackView.distribution = UIStackViewDistributionFillEqually;
    
    [headView addSubview:stackView];
    //创建两个数组存储文字和图片
    NSArray *images = @[@"top_btn01",@"top_btn02",@"top_btn03",@"top_btn04",];
    NSArray *titles = @[@"抽奖",@"秒杀",@"抢红包",@"蜂抱团",];
    for (int i = 0; i < self.iconsArray.count; i++)
    {
        LFIconModel *model = self.iconsArray[i];
        UIButton *button = [UIButton buttonTBImgName:images[i] imageWH:28 title:model.name forSize:12 color:[UIColor blackColor] spacing:5 target:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.backgroundColor = [UIColor whiteColor];
        button.tag = i;

        [stackView addArrangedSubview:button];
    }

}
#pragma mark 添加头部的新闻滚动视图
- (void)setNewsScrollView:(UICollectionReusableView*)header
{
    NSArray *newsArray = @[@"亿元迎新红包",@"年货提前放松",@"红包快速抢"];
    AutoScrollController *autoScrollViewVC = [AutoScrollController new];
    autoScrollViewVC.array = newsArray;
    
    [header addSubview:autoScrollViewVC.view];
    
    [autoScrollViewVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(30);
    }];
    
}
#pragma mark 设置第二个组头视图
- (void)setSecondHeader:(UICollectionReusableView *)headerView
{
    UIView *backView = [[UIView alloc] init];
    backView.backgroundColor = [UIColor redColor];
    UILabel *label = [UILabel labelWithText:@"热卖" andTextColor:[UIColor redColor] andFontSize:16];
    [label sizeToFit];
    UIButton *moreButton = [UIButton buttonLRWithImgName:nil title:@"更多..." color:[UIColor darkGrayColor] forSize:16 target:self action:@selector(moreBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [moreButton sizeToFit];
    moreButton.tag = 10;
    UIButton *imgButton = [UIButton new];
    imgButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"00"]];
    [imgButton addTarget:self action:@selector(moreBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    imgButton.tag = 11;
    
    [headerView addSubview:backView];
    [headerView addSubview:label];
    [headerView addSubview:moreButton];
    [headerView addSubview:imgButton];
    
    //设置布局
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.offset(5);
        make.width.offset(5);
        make.height.offset(20);
    }];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(backView);
        make.left.equalTo(backView.mas_right).offset(10);
        make.height.offset(30);
    }];
    [moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(backView);
        make.right.offset(-5);
        make.height.offset(30);
    }];
    [imgButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(5);
        make.right.offset(-5);
        make.bottom.offset(0);
        make.height.offset(90);
    }];

    
    
}

//按钮点击的方法
- (void)buttonClick:(UIButton *)sender
{
//    NSLog(@"-------------%zd",sender.tag);
    
    LFHomeWebViewController *webVC = [[LFHomeWebViewController alloc] init];
    LFIconModel *model = self.iconsArray[sender.tag];
    webVC.iconModel = model;
    [UIView animateWithDuration:1 animations:^{
        self.view.alpha = 0;
        self.navigationController.navigationBar.alpha = 0;
    } completion:^(BOOL finished) {
        [self.navigationController pushViewController:webVC animated:NO];
        self.view.alpha = 1;
        self.navigationController.navigationBar.alpha = 1;
    }];
}
//第二个组头中按钮的点击方法
- (void)moreBtnClick:(UIButton *)sender
{
    if (sender.tag == 10)
    {//更多按钮的点击
        [self.tabBarController setSelectedIndex:1];
        
    }else
    {//图片按钮的点击
        [self.tabBarController setSelectedIndex:1];
    }
}
#pragma mark - 添加轮播器的方法
- (void)setCycleView:(UICollectionReusableView *)headerView
{
    self.imageArray = [NSMutableArray array];
    for (int i = 0; i < 4; i++)
    {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%02d",i]];
        [self.imageArray addObject:image];
    }
    //CGRectMake(0, 8, Width, (Width - 84) * 9 / 16 + 24)
    NewPagedFlowView *pageFlowView = [[NewPagedFlowView alloc] initWithFrame:CGRectMake(0, 0, Width, (Width - 84) * 9 / 16 + 24)];
    pageFlowView.backgroundColor = [UIColor whiteColor];
    pageFlowView.delegate = self;
    pageFlowView.dataSource = self;
    pageFlowView.minimumPageAlpha = 0.5;
    pageFlowView.minimumPageScale = 0.85;
    pageFlowView.autoTime = 3.0;
    pageFlowView.backgroundColor = [UIColor whiteColor];
    pageFlowView.delegate = self;
    
    //提前告诉有多少页
    pageFlowView.orginPageCount = self.imageArray.count;
    pageFlowView.isOpenAutoScroll = YES;
    
    //初始化pageControl
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, pageFlowView.frame.size.height - 24 - 8, Width, 8)];
    pageFlowView.pageControl = pageControl;
    [pageFlowView addSubview:pageControl];
    /****************************
     使用导航控制器(UINavigationController)
     如果控制器中不存在UIScrollView或者继承自UIScrollView的UI控件
     请使用UIScrollView作为NewPagedFlowView的容器View,才会显示正常,如下
     *****************************/
    [self.collectionView addSubview:pageFlowView];
    
    //第一次的时候调用一下设置背景
    [self didScrollToPage:0 inFlowView:pageFlowView];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#pragma mark NewPagedFlowView的代理方法
- (CGSize)sizeForPageInFlowView:(NewPagedFlowView *)flowView
{
//    return CGSizeMake(Width - 84, (Width - 84) * 9 / 16);
    return CGSizeMake(Width-70, (Width - 84) * 9 / 16);
}
//代理方法
- (void)didScrollToPage:(NSInteger)pageNumber inFlowView:(NewPagedFlowView *)flowView
{
    UIImage *backImage = [self.imageArray[pageNumber] applyLightEffect];
    flowView.backgroundColor = [UIColor colorWithPatternImage:backImage];
    
}
//点击方法
- (void)didSelectCell:(UIView *)subView withSubViewIndex:(NSInteger)subIndex
{
    NSLog(@"点击了第%ld张图",(long)subIndex + 1);
    
}

#pragma mark NewPagedFlowView的数据源方法
- (NSInteger)numberOfPagesInFlowView:(NewPagedFlowView *)flowView {
    return self.imageArray.count;
}

- (UIView *)flowView:(NewPagedFlowView *)flowView cellForPageAtIndex:(NSInteger)index{
    PGIndexBannerSubiew *bannerView = (PGIndexBannerSubiew *)[flowView dequeueReusableCell];
    if (!bannerView) {
        bannerView = [[PGIndexBannerSubiew alloc] initWithFrame:CGRectMake(0, 0, Width - 84, (Width - 84) * 9 / 16)];
        bannerView.layer.cornerRadius = 4;
        bannerView.layer.masksToBounds = YES;
    }
    //在这里下载网络图片
    //  [bannerView.mainImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:hostUrlsImg,imageDict[@"img"]]] placeholderImage:[UIImage imageNamed:@""]];
    bannerView.mainImageView.image = self.imageArray[index];
    return bannerView;
}
//懒加载
- (NSMutableArray *)imageArray {
    if (_imageArray == nil) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}

#pragma mark - 导航控制器中按钮的点击方法
//扫一扫和搜索按钮
- (void)navBtnClick:(UIButton *)sender
{
    
    switch (sender.tag) {
        case 1://扫一扫
            [self scanBtnClick];
            break;
        case 2://选择地址
            [self locationBtnClick];
            break;
        case 3://搜索按钮
            [self searchBtnClick];
            break;
        default:
            break;
    }
}
//选择位置的按钮点击方法
- (void)scanBtnClick
{
    NSLog(@"---扫一扫----");
    LFScanToolController *vc = [[LFScanToolController alloc] init];
    
    if (vc.isCameraValid && vc.isCameraAllowed) {
        vc.scanDelegate = self;
        [self presentViewController:vc animated:YES completion:nil];
    }else{
        if (!vc.isCameraAllowed) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请在设备的设置-隐私-相机中允许访问相机。" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            
            [alertView show];
        }else if (!vc.isCameraValid){
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"虚拟机你扫个毛线啊。" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
        }
        
    }
    
}
//选择位置的按钮点击方法
- (void)locationBtnClick
{
    NSLog(@"---选择位置----");
    [self show:nil];
}
//选择位置的按钮点击方法
- (void)searchBtnClick
{
    NSLog(@"---搜索----");
    
    LFHomeSearchViewController *searchVC = [[LFHomeSearchViewController alloc] init];
    searchVC.allDataArray = self.hotSellerArray ;

    [UIView animateWithDuration:1 animations:^{
        self.view.alpha = 0;
        self.navigationController.navigationBar.alpha = 0;
    } completion:^(BOOL finished) {
        [self.navigationController pushViewController:searchVC animated:NO];
        self.view.alpha = 1;
        self.navigationController.navigationBar.alpha = 1;
        searchVC.navigationController.navigationBar.alpha = 1;
    }];
}
#pragma mark - 地址选择相关的方法
- (void)show:(UIButton *)sender {
//    NSString *address = _addressLabel.text;
    NSString *address = self.selectedSite;
    NSArray *array = [address componentsSeparatedByString:@" "];
    NSString *province = @"";//省
    NSString *city = @"";//市
    NSString *county = @"";//县
    if (array.count > 2) {
        province = array[0];
        city = array[1];
        county = array[2];
    } else if (array.count > 1) {
        province = array[0];
        city = array[1];
    } else if (array.count > 0) {
        province = array[0];
    }
    
    [self.regionPickerView showPickerWithProvinceName:province cityName:city countyName:county];
}

- (HXProvincialCitiesCountiesPickerview *)regionPickerView {
    if (!_regionPickerView) {
        _regionPickerView = [[HXProvincialCitiesCountiesPickerview alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        
        __weak typeof(self) wself = self;
        _regionPickerView.completion = ^(NSString *provinceName,NSString *cityName,NSString *countyName) {
            __strong typeof(wself) self = wself;
            //设置返回过来的数据
//            self.addressLabel.text = [NSString stringWithFormat:@"%@ %@ %@",provinceName,cityName,countyName];
            self.selectedSite = [NSString stringWithFormat:@"%@ %@ %@",provinceName,cityName,countyName];
            NSString *str = [NSString stringWithFormat:@"配送至:%@",self.selectedSite];
            [self.siteButton setTitle:str forState:UIControlStateNormal];
            [self.siteButton setTitle:str forState:UIControlStateHighlighted];
            
        };
        [self.navigationController.view addSubview:_regionPickerView];
    }
    return _regionPickerView;
}
@end
